package com.itwjr.JDBC;

import java.sql.*;

public class Testresultset2 {
    public static void main(String[] args) throws Exception {
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接
        String url = "jdbc:mysql://127.0.0.1:3306/db1";
        String user = "root";
        String psw = "1234";
        Connection connection = DriverManager.getConnection(url, user, psw);
        //定义sql语句
        //String sql = "UPDATE stu SET age = 90 WHERE id = 1";
        String usernameFrom = "huyoufu@itcast.cn' or '1' = '1";
        String sql = "select * from user where  name = ?";
        //获取执行SQL对象
        //执行SQL语句需要SQL执行对象，而这个执行对象就是Statement对象
        //Statement statement = connection.createStatement();
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,usernameFrom);
        //执行 查询语句
       // ResultSet resultSet = statement.executeQuery(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        //执行sql语句
        //判断该次查询是否存在结果
        while (resultSet.next()){
            //有 就取出来 打印一下他
            int id = resultSet.getInt(1);
            System.out.println("id是："+id);
            String name = resultSet.getString(2);
            System.out.println("名字是："+name);
            int age = resultSet.getInt(3);
            System.out.println("年龄是："+age);
        }
        //回收资源
        resultSet.close();
        //statement.close();
        preparedStatement.close();
        connection.close();
    }
}
